﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL;
using ZK.Entity;
using ZK.Entity.Config;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmProductInfo : Form
    {
        public FrmProductInfo()
        {
            InitializeComponent();
        }
        BindingList<ProductInfo> bindinglist = new BindingList<ProductInfo>();
        BindingSource bindingSource = new BindingSource();

        public static EventHandler SaveEvent;

        private void FrmProductInfo_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = bindinglist;
            dataView1.DataSource = bindingSource;
            AddCloumns();
            LoadData();
        }

        private void AddCloumns()
        {
            // dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo), nameof(ProductInfo.Id)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo), nameof(ProductInfo.CustomerName),true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo), nameof(ProductInfo.CustomerName2), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo),nameof(ProductInfo.ProductName), true));

            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo), nameof(ProductInfo.Other), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(ProductInfo), nameof(ProductInfo.TemplateName), true));
            
        }


        private void LoadData()
        {
           var list = ProductInfoBLL.Instance.GetList();
            bindinglist.Clear();

            if (list != null)
            {
                foreach (var item in list)
                {
                    bindinglist.Add(item);
                }
            }
          
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {

            FrmProductEdit frm = new FrmProductEdit();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                frm.ProductInfo.Id = ProductInfoBLL.Instance.GetNewID();

                if (ProductInfoBLL.Instance.Insert(frm.ProductInfo))
                {
                    bindinglist.Add(frm.ProductInfo);
                    SaveEvent(null, null);
                }
               
            }
          
        }


        //private void btnSave_Click(object sender, EventArgs e)
        //{
        //    ProductInfoBLL.Instance.DeleteAll();

        //    if (ProductInfoBLL.Instance.InsertList(bindinglist.ToList()))
        //    {
        //        MessageBox.Show("保存成功");
        //    }
        //    else
        //    {
        //        MessageBox.Show("保存失败");
        //    }

        //    if (SaveEvent != null)
        //    {
        //        SaveEvent(null, null);
        //    }
        //}


        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (ProductInfoBLL.Instance.Delete(bindinglist[dataView1.CurrentCell.RowIndex].Id))
                    {
                        bindinglist.RemoveAt(dataView1.CurrentCell.RowIndex);

                        SaveEvent(null,null);
                    }
                }
              
            }
        }

        private void btnCustomer_Click(object sender, EventArgs e)
        {
            FrmCustomer frm = new FrmCustomer();
            frm.ShowDialog();
            SaveEvent(null, null);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                FrmProductEdit frm = new FrmProductEdit();
                frm.ProductInfo = bindinglist[dataView1.CurrentCell.RowIndex];
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (ProductInfoBLL.Instance.Update(frm.ProductInfo))
                    {
                        bindinglist[dataView1.CurrentCell.RowIndex] = frm.ProductInfo;
                        dataView1.Refresh();
                        SaveEvent(null, null);
                    }

                }
            }
        }
    }
}
